using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.Components;
using MacomberMap.Common.Internals;

namespace MacomberMap.Loader.User_Interfaces.Common
{
    /// <summary>
    /// This class allows the user to edit or create a new substation and/or line.
    /// </summary>
    public partial class frmElementEditor : Form
    {
        #region Variable declarations
        /// <summary>The element to be updated</summary>
        public MM_Element Element;
        #endregion


        #region Initialization
        /// <summary>
        /// Initialize a new element editor
        /// </summary>
        /// <param name="Element">The element to be edited</param>
        public frmElementEditor(MM_Element Element)
        {
            InitializeComponent();
            this.Element = Element;
            pg.SelectedObject = Element;
        }

        /// <summary>
        /// Create a new substation with existing lat/long
        /// </summary>
        /// <param name="LatLng">The latitude and longitude for the new substation</param>
        /// <returns></returns>
        public static frmElementEditor NewSubstation(PointF LatLng)
        {
            MM_Substation NewSub = new MM_Substation();
            NewSub.LatLong = LatLng;            
            return new frmElementEditor(NewSub);
        }

        /// <summary>
        /// Create a new line
        /// </summary>
        /// <returns></returns>
        public static frmElementEditor NewLine()
        {
            MM_Line NewLine = new MM_Line();
            return new frmElementEditor(NewLine);
        }
        #endregion

        #region User Interaction
        /// <summary>
        /// Handle the user's pressing OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Element.Name))
                MessageBox.Show("An MM element must have a name!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if (MM_Repository.TEIDs.ContainsKey(Element.TEID) && MM_Repository.TEIDs[Element.TEID] != Element)
                MessageBox.Show("An element with TEID " + Element.TEID.ToString("#,##0") + " already exists!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error );
            else if (Element is MM_Substation && MM_Repository.Substations.ContainsKey(Element.Name) && MM_Repository.Substations[Element.Name] != Element)
                MessageBox.Show("An substation with name" + Element.Name + " already exists!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if (Element is MM_Line && MM_Repository.Lines.ContainsKey(Element.Name) && MM_Repository.Lines[Element.Name] != Element)
                MessageBox.Show("A line with name" + Element.Name + " already exists!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }

        }
        #endregion
    }
}